﻿package birdry.media
{
	import flash.display.Sprite;
	import flash.events.Event;
	
	import flash.media.Video;
	import flash.net.NetConnection;
	
	
	import flash.display.DisplayObjectContainer;
	
    import flash.net.NetStream;
	import flash.events.NetStatusEvent;
	import flash.media.SoundTransform;
	import flash.events.SecurityErrorEvent;
	
	/**
	* ...
	* @author 
	*/
	public class VideoController extends Sprite	{
		
		public static const COMPLETE:String = "complete";
		public static const SEND_META:String = "send_meta";
		public static const VIDEO_STOP:String = "video_stop";
		
		public var video:Video;
		var connection:NetConnection;
        var stream:NetStream;
		var loop:Boolean;
		var customClient:CustomClient;
		var info:Object;
		var duration:Number;
		var file:String;
		
		public function VideoController(loop:Boolean = true): void	{
			this.loop = loop;
			setLayout();
			addEvent();
		}
		
		private function setLayout():void		{
			connection = new NetConnection();
            connection.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandler);
            connection.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
            connection.connect(null);
		}
		
		private function netStatusHandler(event:NetStatusEvent):void {
            switch (event.info.code) {
                case "NetConnection.Connect.Success":
                    connectStream();
                break;
                case "NetStream.Play.StreamNotFound":
                    //	trace("Stream not found: " + videoURL);
                break;
				case "NetStream.Play.Start":
				break;
                case "NetStream.Play.Stop":
                   if (loop)	stream.play(file);
				   dispatchEvent( new Event( VideoController.VIDEO_STOP ));
                break;
				case "NetStream.Buffer.Full":
				break;
				case "NetStream.Buffer.Empty":
				break;
				case "NetStream.Publish.Start":
					//	trace("NetStream.Publish.Start");
				break;
            }
        }

        private function securityErrorHandler(event:SecurityErrorEvent):void {
            trace("securityErrorHandler: " + event);
        }

        private function connectStream():void {
			stream = new NetStream(connection);
			stream.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandler);
			customClient = new CustomClient();
			stream.client = customClient;
			
			video = new Video();
			customClient.videoController = this;
            video.attachNetStream(stream);
			addChild(video);
        }
		
		public function play( str: String ): void	{
			file = str;
			addEventListener(Event.ENTER_FRAME, onEnter);
			stream.close();
			stream.play( file );
		}
		
		public function load( str: String ): void		{
			stream.close();
			stream.play( str );
			pause();
		}
		
		public function stop(): void	{
			stream.pause();
			stream.seek(0);
		}
		
		public function pause(): void	{
			removeEventListener(Event.ENTER_FRAME, onEnter);
			stream.pause();
		}
		
		public function resume(): void	{
			addEventListener(Event.ENTER_FRAME, onEnter);
			stream.resume();
		}
		
		public function seek(offset:Number): void	{
			stream.seek(offset);
		}
		
		public function get videoWidth(): Number	{
			return video.width;
		}
		public function set videoWidth( value: Number ): void	{
			video.width = value;
		}
		public function get videoHeight(): Number	{
			return video.height;
		}
		public function set videoHeight( value: Number ): void	{
			video.height = value;
		}
		
		public function get volume(): Number	{
			return stream.soundTransform.volume;
		}
		public function set volume( value: Number ): void	{
			stream.soundTransform = new SoundTransform( value );
		}
		private function onEnter( e: Event ): void	{
			if (stream.time >= duration)	{
				stream.pause();
				removeEventListener(Event.ENTER_FRAME, onEnter);
				dispatchEvent(new Event( VideoController.COMPLETE ));
			}
		}
		
		private function addEvent():void		{
			
		}
		
		public function sendMetaData(info:Object): void	{
			this.info = info;
			duration = info.duration;
			dispatchEvent( new Event( VideoController.SEND_META ));
		}
		
	}	// class
}	// package

class CustomClient {
	public var videoController:Object;
	public function onMetaData(info:Object):void {
		videoController.sendMetaData(info);
		//trace("metadata: duration=" + info.duration + " width=" + info.width + " height=" + info.height + " framerate=" + info.framerate);
	}
	public function onCuePoint(info:Object):void {
		//trace("cuepoint: time=" + info.time + " name=" + info.name + " type=" + info.type);
	}
}